package org.link.zuoye1;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * 
 * project_name:ZuoYe
 * package_name:org.link.zuoye1
 * file_name:Test4.java
 * @author Link
 * create_date:2015年8月10日 下午5:54:05
 * modified_date:
 * description:判断邮箱格式是否正确
 *
 */
public class Test4 extends JFrame{
	
	private JLabel label1,label2;
	private JTextField field1,field2;
	private JButton bt1;
	
	public Test4(){
		
		this.setSize(300,200);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		this.setLocation((int)((screenSize.getWidth()-frameSize.getWidth())/2), (int)(screenSize.getHeight()-frameSize.getHeight())/2);
		this.setTitle("邮箱格式");
		ImageIcon tp = new ImageIcon(Test1.class.getResource("/image/3333.jpg"));
		this.setIconImage(tp.getImage());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭同时关闭进程
		this.setResizable(false);//设置窗体不可放大
		
		this.setLayout(null);
		
		label1 = new JLabel("Address");
		label1.setBounds(55, 20, 80, 20);
		this.add(label1);
		
		label2 = new JLabel("result");
		label2.setBounds(210, 20, 50, 20);
		this.add(label2);
		
		field1 = new JTextField();
		field1.setBounds(30, 45, 100, 30);
		field1.setHorizontalAlignment(JTextField.CENTER);//设置文本框居中
		field1.setFont(new Font("微软雅黑",Font.ROMAN_BASELINE,12));
		this.add(field1);
		
		field2 = new JTextField();
		field2.setBounds(180, 45, 100, 30);
		field2.setHorizontalAlignment(JTextField.CENTER);//设置文本框居中
		field2.setFont(new Font("微软雅黑",Font.ROMAN_BASELINE,14));
		this.add(field2);
		
		bt1 = new JButton("正确与否");
		bt1.setBounds(80, 100, 100, 35);
		bt1.setMargin(new Insets(0,0,0,0));//设置按钮的边缘空余部分为0
		this.add(bt1);
		bt1.addActionListener(new ActionListener() {//判断格式是否正确方法
			
			@Override
			public void actionPerformed(ActionEvent e) {
				boolean a = field1.getText().contains(".")&&field1.getText().contains("@");//判断是否含有.和@
				boolean b;
				if((field1.getText().indexOf("@")!=0)&&(field1.getText().indexOf("@")!=(field1.getText().length()-1))&&(field1.getText().indexOf("@")) == field1.getText().lastIndexOf("@")){//判断@出现的位置
					b = true;
				}else{
					b = false;
				}
				boolean c;
				if((field1.getText().indexOf(".")!=0)&&(field1.getText().indexOf(".")!=(field1.getText().length()-1))&&(field1.getText().indexOf(".")) == field1.getText().lastIndexOf(".")){//判断.出现的位置
					c = true;
				}else{
					c = false;
				}
				boolean d;
				if((field1.getText().indexOf("@")+1)<field1.getText().indexOf(".")){//判断.和@中间是否有东西
					d = true;
				}else{
					d = false;
				}
				
				if(a == true&&b == true&&c == true&&d == true){
					field2.setText("格式正确!");
				}else{
					field2.setText("格式错误!");
				}
			}
		});
		
	}
	
	public static void main(String[] args) {
		Test4 t4 = new Test4();
		t4.setVisible(true);
		
	}
}




